<?php

namespace System\Lib;

/**
 * Description of MasterPage
 *
 * @author elton.schivei
 */
abstract class MasterPage extends \System\Core\View
{

    /**
     * @var \System\Core\View
     */
    private $_views = array();
    private $_disabled = false;

    final public function __construct()
    {
        parent::__construct();
        $this->Init();
        Log::Write('Master Page View Class Loaded', Log::DEBUG);
    }

    abstract protected function Init();

    /**
     * Add or Update View Web Part on Master Page View
     * @param \System\Types\String $key
     * @param \System\Core\View $view
     * @return boolean
     */
    final public function AddOrUpdateView(\System\Types\String $key, \System\Core\View $view)
    {
        if (!\preg_match('/^([_a-z])([_a-z0-9]){0,}$/g', $key->ToString()))
            return false;

        $view->SetMaster($this);

        $this->_views[$key] = $view;

        return true;
    }

    final public function CreateBuffer()
    {
        if ($this->_disabled)
            return null;

        /* @var $key \System\Types\String */
        /* @var $view \System\Core\View */
        foreach ($this->_views as $key => $view)
        {
            $this->_data[$key->ToString()] = $view->CreateBuffer();
        }

        return parent::CreateBuffer();
    }

    public function __destruct()
    {
        if (!$this->_disposed && !$this->_disabled)
            $this->Dispose();
    }

    public function Enable()
    {
        $this->_disabled = false;
    }

    public function Disable()
    {
        $this->_disabled = true;
    }

    public function Disabled()
    {
        return $this->_disabled;
    }

}
